﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace GooseTest
{
    class Op
    {
        /// <summary>
        /// 定义上传数据
        /// </summary>
        public static List<String> cardIdList = new List<string>();//卡的id链表
        public static Dictionary<String,long> farmMap=new Dictionary<string,long>();//农场字典, 根据农场名称取农场id
        public static Dictionary<String, String> farmidMap = new Dictionary<String, String>();//农场id字典，根据id取农场名字
        public static Dictionary<String, long> retailerMap = new Dictionary<string, long>();//销售商字典   
        public static Dictionary<String, String> retaileridMap = new Dictionary<String, String>();//销售商字典,根据id取销售商名字
        public static string op = "请选择";

        public static long farmId = 0;
        public static long retailerId = 0;    
        public static Double totalWeight = 0;//总重
        public static Double unitPrice = 0;//单价
        public static string cardId=null;

        public static Boolean cheakDouble(string s)
        {
            String pattern = @"^\d+\.?\d*$";
            return Regex.IsMatch(s, pattern);
        }

        public static Boolean cheakLong(string s)
        {
            String pattern = @"^\d+$";
            return Regex.IsMatch(s, pattern);
        }

 
        /// <summary>
        /// 读取卡的序列号，返回string，否则返回null
        /// 注意： 读取卡号先检验第五区密码再返回卡的序列号
        /// </summary>
        public static int readByDh()
        {
           
            Op.cardId= null;
            int ret = API_RF.rf_Check(5);//校验第五区密码           
            if (ret == 0)
            {
                Op.cardId = API_RF.snr + "";
            }
            return ret;
        }


        /// <summary>
        /// 保存到文件：文件保存格式：
        /// 1 农场id
        /// 2 销售商id
        /// 3...card id
        /// </summary>
        /// <typeparam name="T">保存类型</typeparam>
        /// <param name="filePath">保存的位置以及名称</param>
        /// <param name="obj">对象</param>
        /// <param name="append">是否追加，false表示覆盖，true表示追加</param>
        /// <returns>保存成功则返回true</returns>
        public static Boolean save<T>(String filePath, List<T> obj, Boolean append)
        {
            using (StreamWriter sw = new StreamWriter(filePath, append))
            {
                try
                {
                    foreach (T s in obj)
                    {
                        sw.WriteLine(s);
                    }
                    return true;
                }
                catch (Exception e)
                {
                    List<string> l = new List<string>();
                    l.Add(e.Message);
                    save(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + "\\log.txt", l, true);
                    return false;
                }
            }
        }


        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<string> readFile(string filePath)
        {
            List<string> obj = new List<string>();
            string s = null;
            try
            {
                using (StreamReader sr = new StreamReader(filePath))
                {                   
                    while (!sr.EndOfStream)
                    {
                        s = sr.ReadLine();
                        obj.Add(s);
                    }
                }
            }
            catch (Exception e)
            {
                List<string> l = new List<string>();
                l.Add(e.Message);
                save(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName)  +"\\log.txt", l, true);
            }
            return obj;
        }

    }
}
